{
  "cells": [
    {
      "cell_type": "markdown",
      "id": "cell_0",
      "metadata": {},
      "source": [
        "# Quick Start Example - Extracting payment terms from a document"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "cell_1",
      "metadata": {},
      "outputs": [],
      "source": [
        "%pip install -U contextgem"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "cell_2",
      "metadata": {},
      "source": [
        "To run the extraction, please provide your LLM details in the ``DocumentLLM(...)`` constructor further below."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "cell_3",
      "metadata": {},
      "outputs": [],
      "source": [
        "# Quick Start Example - Extracting payment terms from a document\n",
        "\n",
        "import os\n",
        "\n",
        "from contextgem import Aspect, Document, DocumentLLM\n",
        "\n",
        "\n",
        "# Sample document text (shortened for brevity)\n",
        "doc = Document(\n",
        "    raw_text=(\n",
        "        \"SERVICE AGREEMENT\\n\"\n",
        "        \"SERVICES. Provider agrees to provide the following services to Client: \"\n",
        "        \"Cloud-based data analytics platform access and maintenance...\\n\"\n",
        "        \"PAYMENT. Client agrees to pay $5,000 per month for the services. \"\n",
        "        \"Payment is due on the 1st of each month. Late payments will incur a 2% fee per month...\\n\"\n",
        "        \"CONFIDENTIALITY. Both parties agree to keep all proprietary information confidential \"\n",
        "        \"for a period of 5 years following termination of this Agreement...\"\n",
        "    ),\n",
        ")\n",
        "\n",
        "# Define the aspects to extract\n",
        "doc.aspects = [\n",
        "    Aspect(\n",
        "        name=\"Payment Terms\",\n",
        "        description=\"Payment terms and conditions in the contract\",\n",
        "        # see the docs for more configuration options, e.g. sub-aspects, concepts, etc.\n",
        "    ),\n",
        "    # Add more aspects as needed\n",
        "]\n",
        "# Or use `doc.add_aspects([...])`\n",
        "\n",
        "# Define an LLM for extracting information from the document\n",
        "llm = DocumentLLM(\n",
        "    model=\"openai/gpt-4o-mini\",  # or another provider/LLM\n",
        "    api_key=os.environ.get(\n",
        "        \"CONTEXTGEM_OPENAI_API_KEY\"\n",
        "    ),  # your API key for the LLM provider\n",
        "    # see the docs for more configuration options\n",
        ")\n",
        "\n",
        "# Extract information from the document\n",
        "doc = llm.extract_all(doc)  # or use async version `await llm.extract_all_async(doc)`\n",
        "\n",
        "# Access extracted information in the document object\n",
        "for item in doc.aspects[0].extracted_items:\n",
        "    print(f\"\u2022 {item.value}\")\n",
        "# or `doc.get_aspect_by_name(\"Payment Terms\").extracted_items`\n",
        "\n",
        "# Output (exact paragraphs from the document):\n",
        "# \u2022 PAYMENT. Client agrees to pay $5,000 per month for the services. Payment is due on the 1st of each month. Late payments will incur a 2% fee per month...\n"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.10.0"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}